<%@ page import="com.touzi.constants.MobileCons" %>
<%@ page import="com.touzi.utils.MyHTTPUtils" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.touzi.constants.DbCons" %>
<%@ page import="com.touzi.mapper.UserMapper" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactory" %>
<%@ page import="com.touzi.beans.dbs.users.User" %>
<%@ page import="com.touzi.mapper.DingdanMapper" %>
<%@ page import="java.util.List" %>
<%@ page import="com.touzi.beans.dbs.dingdan.AUserDingd" %><%--
  Created by IntelliJ IDEA.
  User: w7851
  Date: 2018/5/16
  Time: 7:49
  修改密码页面
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

<%
    Cookie usercookie = MyHTTPUtils.getCookieByName(request, MobileCons.COOKIE_USERNAME);
    String userphone = usercookie.getValue();
    SqlSession sqlSession = ((SqlSessionFactory) application.getAttribute(DbCons.DB_FACTORY)).openSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User user = userMapper.findUserByDhmw(userphone).get(0);

    sqlSession.close();

    request.setAttribute("user", user);
%>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改密码和支付宝</title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>

    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>

    <script src="/js/jquery-3.3.1.min.js"></script>
    <link rel="stylesheet" href="/css/ydui.css">
    <script src="/js/ydui.flexible.js"></script>
    <style>
        .errortip {
            color: red;
            margin-left: 8px;
            display: none;
        }
    </style>
</head>
<body>
<div class="g-flexview">
    <header class="m-navbar  navbar-fixed">
        <a onclick="goback()" class="navbar-item">
            <i class="back-ico"></i>
        </a>
        <div class="navbar-center"><span class="navbar-title">修改密码和支付宝</span></div>
    </header>


    <div class="g-scrollview" style="margin-top: 1rem">
        <div class="m-celltitle">修改密码和支付宝</div>

        <form action="/mobile" method="post">
            <input name="dianhua" value="${user.dianhua}" style="display: none">
            <input name="method" value="xiugaimmhezfb" style="display: none">
            <div class="m-cell">
                <div class="cell-item">
                    <div class="cell-left">原密码：</div>
                    <div class="cell-right"><input id="origpasswd" name="origpasswd" type="password"
                                                   placeholder="请输入原来的密码"
                                                   class="cell-input"
                                                   autocomplete="off"/></div>
                </div>

                <div class="cell-item">
                    <div class="cell-left">新密码：</div>
                    <div class="cell-right"><input id="newpasswd" name="newpasswd" type="password"
                                                   placeholder="请输入至少6位新密码"
                                                   class="cell-input"
                                                   autocomplete="off"/></div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">确认密码：</div>
                    <div class="cell-right"><input id="newpasswd2" type="password" placeholder="再次输入新密码"
                                                   class="cell-input"
                                                   autocomplete="off"/></div>
                </div>
                <div class="cell-item">
                    <div class="cell-left">支付宝：</div>
                    <div class="cell-right"><input id="aliaccount" name="newaliaccount" type="text"
                                                   placeholder="用于提现的支付宝账号"
                                                   value="${user.zhifubao}"
                                                   class="cell-input"
                                                   autocomplete="off"/></div>
                </div>

                <div class="cell-item">
                    <div class="cell-right">
                        <input type="text" id="code" class="cell-input" placeholder="请输入验证码" autocomplete="off">
                        <img src="/mobile?method=getyzm" style="width: 150px;height: 40px;margin-top: 8px"
                             onclick="changeImg()">
                    </div>
                </div>
            </div>

            <div class="errors" style="">
                <p id="errortip" class="errortip">金额有误</p>
            </div>

            <div style="padding: 0 16px">
                <input type="submit" class="btn-block btn-primary" value="确认修改"></input>
            </div>

        </form>

    </div>
</div>
</body>
<script>

    function isOrigPassOk() {
        var ok = false;
        var code = $("#code");
        var value = Number(code.val());

        if (value == "") {
            $("#errortip").show().html("请先输入验证码");
            ok = false;
        } else {
            $("#errortip").hide();
            ok = true;
        }

        $.ajax(
            {
                type: "POST",
                async: false,
                url: "/mobile",
                data: {
                    "method": "verifylogin",
                    "phone": ${user.dianhua},
                    "passwd": $("#origpasswd").val(),
                    "code": code.val()
                },
                success: function (data) {
                    console.log(data);
                    var correct = "ok";
                    if (data == correct) {
                        ok = true;
                    } else {
                        ok = false;
                    }
                }
            }
        );

        return ok;
    }

    function isPasswdOK() {
        var passwd = $("#newpasswd");
        var passwd2 = $("#newpasswd2");

        if (passwd.val().length < 6) {
            return false;
        }

        if (!(passwd.val() == passwd2.val())) {
            return false;
        }

        return true;
    }


    $("form").submit(function () {
        var tips = "提示";
        var pass = isPasswdOK();
        var ori = isOrigPassOk();

        if (!ori) {
            tips = tips + " 【原密码或验证码错误】";
        }
        if (!pass) {
            tips = tips + " 【新密码格式错误或者两次输入不一致】";
        }

        if (ori && pass) {

        } else {
            $("#errortip").show().html(tips);
            return false;
        }

    });

    function goback() {
        history.go(-1);
    }

    function changeImg() {
        var imgSrc = $("img");
        var src = "/mobile?method=getyzm&t=" + (new Date().getTime());
        console.log(src);
        imgSrc.attr("src", src);
    }
</script>
</html>
